import html2Canvas from "html2canvas"
import JsPDF from "jspdf"

export default {
    install(Vue, options) {
        //html页面生成PDF文档
        Vue.prototype.getPdf = function(viewId, name) {
                let canvasId = viewId ? document.getElementById(viewId) : document.body; //实现自定义可传定义dom 不传默认为body
                html2Canvas(canvasId, {
                    allowTaint: true
                }).then(canvas => {
                    let contentWidth = canvas.width;
                    let contentHeight = canvas.height;
                    let pageHeight = contentWidth / 592.28 * 841.89;
                    let leftHeight = contentHeight
                    let position = 0
                    let imgWidth = 595.28
                    let imgHeight = 592.28 / contentWidth * contentHeight
                    let pageData = canvas.toDataURL('image/jpeg', 1.0)
                    let PDF = new JsPDF('', 'pt', 'a4')
                    if (leftHeight < pageHeight) {
                        PDF.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight)
                    } else {
                        while (leftHeight > 0) {
                            PDF.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
                            leftHeight -= pageHeight
                            position -= 841.89
                            if (leftHeight > 0) {
                                PDF.addPage()
                            }
                        }
                    }
                    let timer = (new Date()).getTime(); //文件名
                    PDF.save(name || timer + '.pdf')

                })
            }
            //获取html图片转格式化
        function DataURLToBlob(dataUrl) {
            var arr = dataUrl.split(','),
                mime = arr[0].match(/(:(.*?);)/)[1],
                bstr = atob(arr[1]),
                n = bstr.length,
                u8arr = new Uint8Array(n);
            while (n--) {
                u8arr[n] = bstr.charCodeAt(n);
            }
            return new Blob([u8arr], { type: mime });
        }

        //下载html生成图片
        Vue.prototype.downloadImg = function(viewId, name) {
            // let canvasId = document.body; //导出body下面的所有内容 可以根据实际情况替换
            // let canvasId = document.getElementById("pdfDom"); //例子
            let canvasId = viewId ? document.getElementById(viewId) : document.body; //实现自定义可传定义dom 不传默认为body
            let a = document.createElement('a');
            html2Canvas(canvasId).then(canvas => {
                let dom = document.body.appendChild(canvas);
                dom.style.display = "none";
                a.style.display = "none";
                document.body.removeChild(dom);
                let blob = DataURLToBlob(dom.toDataURL("iamge/png"));
                a.setAttribute("href", URL.createObjectURL(blob));
                a.setAttribute("download", name + ".png");
                document.body.appendChild(a);
                a.click();
                URL.revokeObjectURL(blob);
                document.body.removeChild(a);
            })
        }

        // 获取打印图片
        Vue.prototype.printImg = function(viewId, name) {
            // $(window).scrollTop(0); // jQuery 的方法
            document.body.scrollTop = 0; // IE的
            document.documentElement.scrollTop = 0; // 其他
            this.downloadImg(viewId, name)
        }
    }
}